package com.smart_campus.campus.controller;


import com.smart_campus.campus.domain.CampusJournalism;
import com.smart_campus.campus.domain.CampusMedia;
import com.smart_campus.campus.service.CampusMediaService;
import com.smart_campus.campus.vo.CampusMideaVo;
import com.smart_campus.sys.common.ActiveUser;
import com.smart_campus.sys.common.ResultObj;
import com.smart_campus.sys.domain.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author Alfred
 * @time: 2020/6/28 19:27
 */
@RestController
@RequestMapping("media")
public class CampusMediaController {

    @Autowired
    private CampusMediaService campusMediaService;

    /**
     * 查询媒体
     * @return CampusMidiaVo
     */
    @RequestMapping("queryMidia")
    public Object queryMidia(CampusMideaVo campusMidiaVo){

        return this.campusMediaService.queryMidia(campusMidiaVo);
    }

    /**
     * 添加媒体新闻
     * @param campusMideaVo
     * @return
     */
    @RequestMapping("addMidia")
    public ResultObj addMidia(CampusMideaVo campusMideaVo){
        try {
            Subject subject = SecurityUtils.getSubject();
            ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
            User user = activeUser.getUser();
            campusMideaVo.setUserId(user.getUserId());
            campusMideaVo.setCreateTime(new Date());
            Integer integer = this.campusMediaService.queryMax();
            campusMideaVo.setOrdernum(integer);
            this.campusMediaService.insertSelective(campusMideaVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 删除媒体新闻
     * @param id
     * @return
     */
    @RequestMapping("deleteMidia")
    public ResultObj deleteMidia(Integer id){
        try {
            this.campusMediaService.deleteByPrimaryKey(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }


    /**
     * 批量删除媒体新闻
     * @param ids
     * @return
     */
    @RequestMapping("deleteBatchMidia")
    public ResultObj deleteBatchMidia(Integer ids[]){
        try {
            for (Integer id: ids) {
                this.campusMediaService.deleteByPrimaryKey(id);
            }
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 修改媒体
     * @param
     * @return
     */
    @RequestMapping("updateMidia")
    public ResultObj updateMidia(CampusMedia campusMidea){
        try {
           this.campusMediaService.updateByPrimaryKeySelective(campusMidea);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 查询单个
     * @param campusMedia
     * @return
     */
    @RequestMapping("selectMedia")
    public CampusMedia selectJournalism(CampusMedia campusMedia){
        try {
            campusMedia=this.campusMediaService.selectByPrimaryKey(campusMedia.getId());
            return campusMedia;
        } catch (Exception e) {
            e.printStackTrace();
            return campusMedia;
        }
    }

}
